﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SQLHelper;

public partial class front_Show : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["id"]))
        {
            Response.Redirect("~/front/Default.aspx");
        }
        base_bind();
        bind();
    }

    protected void Create_Comment(object sender, EventArgs e)
    {
        if(Master.Role == null)
        {
            Response.Redirect("~/account/Login.aspx");
        }
        string name = Master.ProfileArr[1];
        string comments = TextBox1.Text.ToString().Trim();
        int user_id = Convert.ToInt32(Master.ProfileArr[0]);
        int news_id = Convert.ToInt32(Request["id"]);
        string user_img = Master.ProfileArr[4];
        DateTime created_at = DateTime.Now;
        string sql = "insert into comments (name,comments,user_id,news_id,user_img,created_at) values('"
            + name + "','"
            + comments + "','"
            + user_id + "','"
            + news_id + "','"
            + user_img + "','"
            + created_at + "')";
        DB.GetDataNoQuery(sql);
        DB.ColseSqlCon();
        Response.Redirect("~/front/Show.aspx?id="+ Convert.ToInt32(Request["id"]));
    }

    void bind()
    {
        string sql = "select * from news where id = " + Convert.ToInt32(Request["id"]);
        DataSet res = DB.GetDataWithQuerySecond(sql, "news");
        DataList1.DataSource = res;
        DataList1.DataBind();
        DB.ColseSqlCon();

        string sql1 = "select * from comments where news_id = " + Convert.ToInt32(Request["id"]);
        DataSet res1 = DB.GetDataWithQuerySecond(sql1, "comments");
        DataList4.DataSource = res1;
        DataList4.DataBind();
        DB.ColseSqlCon();
    }

    void base_bind()
    {
        string sql2 = "select top 2 id,title,column_name,source,created_at from news where column_name = '人工智能'";
        DataSet res2 = DB.GetDataWithQuerySecond(sql2, "news");
        DataList2.DataSource = res2;
        DataList2.DataBind();
        DB.ColseSqlCon();

        string sql3 = "select top 2 id,title,column_name,source,created_at from news where column_name = '虚拟现实'";
        DataSet res3 = DB.GetDataWithQuerySecond(sql3, "news");
        DataList3.DataSource = res3;
        DataList3.DataBind();
        DB.ColseSqlCon();
    }
}